#include "Model.h"

CModel::CModel(void)
{
	m_PlayState = TITLE;
	//m_PlayState = NEWGAME;
	m_iNext = 1;
	m_bIsFirst = true;

	m_iMapWidth = 0;
	m_iMapHeight = 0;
	m_iWidth = 0;
	m_iHeight = 0;
}

CModel::~CModel(void)
{
}

int CModel::initialization(int width, int height)
{
	m_iWidth = width;
	m_iHeight = height;

	// read map file
	ifstream read;
	read.open("map.txt");

	m_vMap.resize((width / 5) * (height / 5));
	for (int h = 0; h < m_vMap.size(); h++)
			read >> m_vMap[h];
	read.close();

	m_iMapWidth = width / 5;
	m_iMapHeight = height /5;

	return OK;
}

bool CModel::Update(int input)
{
	if (input == SPACE && m_PlayState == TITLE)
		m_PlayState = MENU;
	else if (input == NUM_4 && m_PlayState == MENU)
		return true; // exit
	else if (input == NUM_1 && m_PlayState == MENU)
		m_PlayState = NEWGAME;
	else if (input == NUM_3 && m_PlayState == MENU)
		m_PlayState = HELP;
	else if (input == SPACE && m_PlayState == HELP)
		m_PlayState = MENU;

	// number of players
	else if (input == NUM_2 && m_PlayState == NEWGAME)
		SetNumberPlayer(2);
	else if (input == NUM_3 && m_PlayState == NEWGAME)
		SetNumberPlayer(3);
	else if (input == NUM_4 && m_PlayState == NEWGAME)
		SetNumberPlayer(4);
	else if (input == NUM_5 && m_PlayState == NEWGAME)
		SetNumberPlayer(5);
	else if (input == NUM_6 && m_PlayState == NEWGAME)
		SetNumberPlayer(6);

	// set up players
	else if (input == NUM_1 && m_PlayState == SETPLAYER) // human
		SetHuman();
	else if (input == NUM_2 && m_PlayState == SETPLAYER) // cpu
		SetCPU();

	else if (input == SPACE && m_PlayState == STATUS && m_bIsFirst)
	{
		//SetPositionCPU();
		m_PlayState = SETPOSITION;
		m_bIsFirst = false;
		m_iNext = 1;
	}

	// switch game and status
	else if (input == SPACE && m_PlayState == STATUS)
		m_PlayState = GAME;
	else if (input == LETTER_V && m_PlayState == GAME)
		m_PlayState = STATUS;

	// set up start position
	else if (input == NUM_1 && m_PlayState == SETPOSITION)
		SetPosition(1);
	else if (input == NUM_2 && m_PlayState == SETPOSITION)
		SetPosition(2);
	else if (input == NUM_3 && m_PlayState == SETPOSITION)
		SetPosition(3);
	else if (input == NUM_4 && m_PlayState == SETPOSITION)
		SetPosition(4);
	else if (input == SPACE && m_PlayState == SETPOSITION)
		SetPositionCPU(); // if it is cpu 

	else if (m_PlayState == GAME)
		Game(input);

	else if (m_PlayState == WINNING)
		return true;


	return false;
}

int CModel::Game(int input)
{
	// get next first then look at either cpu and human
	vector<CHuman>::iterator Human_iter;
	vector<CCPU>::iterator CPU_iter;

	for (Human_iter = m_Human.begin(); Human_iter != m_Human.end(); Human_iter++)
	{
		if (Human_iter->GetOrder() == m_iNext)
		{
			if (input == SPACE && Human_iter->GetState() == MOVE)
			{
				Human_iter->SetMove(Random(1, 6));
				Human_iter->ClearCount();
				CheckDirection(Human_iter);
				MoveIcon(Human_iter);
				return OK;
			}
			if (Human_iter->GetState() == DIRECTION)
			{
				bool right = false;
				bool left = false;
				bool up = false;
				bool down = false;

				//CheckDirection(Human_iter);
				Human_iter->GetPossibleDirection(right, left, up, down);

				if (input == RIGHTARROW && right)
				{
					Human_iter->SetDirection(EAST);
					Human_iter->Ask();
				}
				else if (input == LEFTARROW && left)
				{
					Human_iter->SetDirection(WEST);
					Human_iter->Ask();
				}
				else if (input == UPARROW && up)
				{
					Human_iter->SetDirection(NORTH);
					Human_iter->Ask();
				}
				else if (input == DOWNARROW && down)
				{
					Human_iter->SetDirection(SOUTH);
					Human_iter->Ask();
				}

				MoveIcon(Human_iter);
			}
			if (input == SPACE && Human_iter->GetState() == END) // end of play
			{
				Human_iter->SetState(MOVE);
				Human_iter->SetRob(0);
				if (++m_iNext > m_iNumberPlayers)
					m_iNext = 1;		
				return OK;
			}
		}
	}

	for (CPU_iter = m_CPU.begin(); CPU_iter != m_CPU.end(); CPU_iter++)
	{
		if (CPU_iter->GetOrder() == m_iNext)
		{
			if (input == SPACE && CPU_iter->GetState() == _SPACEBAR)
			{
				MoveIcon(CPU_iter);
				return OK;
			}
			if (input == SPACE && CPU_iter->GetState() == _END)
			{
				CPU_iter->SetState(_SPACEBAR);
				CPU_iter->SetRob(0);
				if (++m_iNext > m_iNumberPlayers)
					m_iNext = 1;
				return OK;
			}
		}
	}
	return OK;
}

int CModel::MoveIcon(vector<CCPU>::iterator CPU)
{
	POINT a;
	bool right = false;
	bool left = false;
	bool up = false;
	bool down = false;
	bool JustAsk = false;

	if (CPU->GetDirection() == NOWHERE)
	{
		CheckDirection(CPU);
		DirectionCPU(CPU);
		JustAsk = true;
	}

	int result = Random(1, 6);
	int count = 0;

	while(count < result)
	{
		right = false;
		left = false;
		up = false;
		down = false;

		// check if it crosses the safe house with enough money
		a = CPU->GetPosition();
		if (m_vMap[m_iMapWidth * a.y + a.x] == '7' && CPU->GetBalance() >= MAX)
		{
			m_PlayState = WINNING;
			return OK;
		}

		CheckDirection(CPU);
		CPU->GetPossibleDirection(right, left, up, down);

		//-------------------------------------------

		if (CPU->GetDirection() == NORTH && JustAsk)
		{
			a = CPU->GetPosition();
			a.y--;
			CPU->SetPosition(a);
			count++;;
			JustAsk = false;
		}
		else if (CPU->GetDirection() == NORTH && up && !right && !left)
		{
			a = CPU->GetPosition();
			a.y--;
			CPU->SetPosition(a);
			count++;;
		}
		else if (CPU->GetDirection() == NORTH && !up)
		{
			if (!right && left)
			{
				CPU->SetDirection(WEST);
				a = CPU->GetPosition();
				a.x--;
				CPU->SetPosition(a);
				count++;;
			}
			else if (right && !left)
			{
				CPU->SetDirection(EAST);
				a = CPU->GetPosition();
				a.x++;
				CPU->SetPosition(a);
				count++;;
			}
			else if (right && left)
			{
				CPU->SetPossibleDirection(right, left, false, false);
				DirectionCPU(CPU);
				JustAsk = true;
			}
		}
		else if (CPU->GetDirection() == NORTH && up && !right && left)
		{
			CPU->SetPossibleDirection(false, left, up, false);
			DirectionCPU(CPU);
			JustAsk = true;
		}
		else if (CPU->GetDirection() == NORTH && up && right && !left)
		{
			CPU->SetPossibleDirection(right, false, up, false);
			DirectionCPU(CPU);
			JustAsk = true;
		}
		else if (CPU->GetDirection() == NORTH && up && right && left)
		{
			CPU->SetPossibleDirection(right, left, up, false);
			DirectionCPU(CPU);
			JustAsk = true;			
		}

		//--------------------------------------

		else if (CPU->GetDirection() == EAST && JustAsk)
		{
			a = CPU->GetPosition();
			a.x++;
			CPU->SetPosition(a);
			count++;;
			JustAsk = false;
		}
		else if (CPU->GetDirection() == EAST && right && !up && !down)
		{
			a = CPU->GetPosition();
			a.x++;
			CPU->SetPosition(a);
			count++;;
		}
		else if (CPU->GetDirection() == EAST && !right)
		{
			if (!up && down)
			{
				CPU->SetDirection(SOUTH);
				a = CPU->GetPosition();
				a.y++;
				CPU->SetPosition(a);
				count++;;
			}
			else if (up && !down)
			{
				CPU->SetDirection(NORTH);
				a = CPU->GetPosition();
				a.y--;
				CPU->SetPosition(a);
				count++;;
			}
			else if (up && down)
			{
				CPU->SetPossibleDirection(false, false, up, down);
				DirectionCPU(CPU);
				JustAsk = true;				
			}
		}
		else if (CPU->GetDirection() == EAST && up && !down && right)
		{
			CPU->SetPossibleDirection(right, false, up, false);
			DirectionCPU(CPU);
			JustAsk = true;			
		}
		else if (CPU->GetDirection() == EAST && !up && down && right)
		{
			CPU->SetPossibleDirection(right, false, false, down);
			DirectionCPU(CPU);
			JustAsk = true;			
		}
		else if (CPU->GetDirection() == EAST && up && down && right)
		{
			CPU->SetPossibleDirection(right, false, up, down);
			DirectionCPU(CPU);
			JustAsk = true;			
		}

		//-----------------------------------------------

		else if (CPU->GetDirection() == SOUTH && JustAsk)
		{
			a = CPU->GetPosition();
			a.y++;
			CPU->SetPosition(a);
			count++;;
			JustAsk = false;
		}
		else if (CPU->GetDirection() == SOUTH && down && !right && !left)
		{
			a = CPU->GetPosition();
			a.y++;
			CPU->SetPosition(a);
			count++;;
		}
		else if (CPU->GetDirection() == SOUTH && !down)
		{
			if (!right && left)
			{
				CPU->SetDirection(WEST);
				a = CPU->GetPosition();
				a.x--;
				CPU->SetPosition(a);
				count++;;
			}
			else if (right && !left)
			{
				CPU->SetDirection(EAST);
				a = CPU->GetPosition();
				a.x++;
				CPU->SetPosition(a);
				count++;;
			}
			else if (right && left)
			{
				CPU->SetPossibleDirection(right, left, false, false);
				DirectionCPU(CPU);
				JustAsk = true;				
			}
		}
		else if (CPU->GetDirection() == SOUTH && down && !right && left)
		{
			CPU->SetPossibleDirection(false, left, false, down);
			DirectionCPU(CPU);
			JustAsk = true;			
		}
		else if (CPU->GetDirection() == SOUTH && down && right && !left)
		{
			CPU->SetPossibleDirection(right, false, false, down);
			DirectionCPU(CPU);
			JustAsk = true;			
		}
		else if (CPU->GetDirection() == SOUTH && down && right && left)
		{
			CPU->SetPossibleDirection(right, left, false, down);
			DirectionCPU(CPU);
			JustAsk = true;			
		}

		//--------------------------------------

		else if (CPU->GetDirection() == WEST && JustAsk)
		{
			a = CPU->GetPosition();
			a.x--;
			CPU->SetPosition(a);
			count++;;
			JustAsk = false;
		}
		else if (CPU->GetDirection() == WEST && left && !up && !down)
		{
			a = CPU->GetPosition();
			a.x--;
			CPU->SetPosition(a);
			count++;;
		}
		else if (CPU->GetDirection() == WEST && !left)
		{
			if (!up && down)
			{
				CPU->SetDirection(SOUTH);
				a = CPU->GetPosition();
				a.y++;
				CPU->SetPosition(a);
				count++;;
			}
			else if (up && !down)
			{
				CPU->SetDirection(NORTH);
				a = CPU->GetPosition();
				a.y--;
				CPU->SetPosition(a);
				count++;;
			}
			else if (up && down)
			{
				CPU->SetPossibleDirection(false, false, up, down);
				DirectionCPU(CPU);
				JustAsk = true;				
			}
		}
		else if (CPU->GetDirection() == WEST && !up && down && left)
		{
			CPU->SetPossibleDirection(false, left, false, down);
			DirectionCPU(CPU);
			JustAsk = true;			
		}
		else if (CPU->GetDirection() == WEST && up && !down && left)
		{
			CPU->SetPossibleDirection(false, left, up, false);
			DirectionCPU(CPU);
			JustAsk = true;			
		}
		else if (CPU->GetDirection() == WEST && up && down && left)
		{
			CPU->SetPossibleDirection(false, left, up, down);
			DirectionCPU(CPU);
			JustAsk = true;			
		}
	
		//----------------------------------------------------------

	}

	a = CPU->GetPosition();
	if (m_vMap[m_iMapWidth * a.y + a.x] == '3')
	{
		CPU->AddRob(5);
		CPU->AddBalance(5);
		CPU->SetState(_END);
	}
	else if (m_vMap[m_iMapWidth * a.y + a.x] == '2')
	{
		CPU->AddRob(10);
		CPU->AddBalance(10);
		CPU->SetState(_END);
	}
	else if (m_vMap[m_iMapWidth * a.y + a.x] == '4')
	{
		CPU->AddRob(20);
		CPU->AddBalance(20);
		CPU->SetState(_END);
	}
	else if (m_vMap[m_iMapWidth * a.y + a.x] == '5')
	{
		Tip();
		CPU->SetState(_END);
	}
	else if (m_vMap[m_iMapWidth * a.y + a.x] == '6')
	{
		Theft(CPU);
	}
	else
		CPU->SetState(_END);

	return OK;
}

int CModel::Tip()
{
	int result = Random(1, m_iNumberPlayers);
	bool up = false;
	bool down = false;
	bool right = false;
	bool left = false;

	vector<CCPU>::iterator CPU_iter;
	vector<CHuman>::iterator Human_iter;

	for (Human_iter = m_Human.begin(); Human_iter != m_Human.end(); Human_iter++)
	{
		if  (Human_iter->GetOrder() == result)
		{
			CheckDirection(Human_iter);
			while(true)
			{
				Human_iter->GetPossibleDirection(right, left, up, down);
				result = Random(1, 4);
				if (result == 1 && right)
				{
					Human_iter->SetDirection(EAST);
					return OK;
				}
				else if (result == 2 && left)
				{
					Human_iter->SetDirection(WEST);
					return OK;
				}
				else if (result == 3 && up)
				{
					Human_iter->SetDirection(NORTH);
					return OK;
				}
				else if (result == 4 && down)
				{
					Human_iter->SetDirection(SOUTH);
					return OK;
				}
			}
		}
	}

	for (CPU_iter = m_CPU.begin(); CPU_iter != m_CPU.end(); CPU_iter++)
	{
		if (CPU_iter->GetOrder() == result)
		{
			CheckDirection(CPU_iter);
			while(true)
			{
				CPU_iter->GetPossibleDirection(right, left, up, down);
				result = Random(1, 4);
				if (result == 1 && right)
				{
					CPU_iter->SetDirection(EAST);
					return OK;
				}
				else if (result == 2 && left)
				{
					CPU_iter->SetDirection(WEST);
					return OK;
				}
				else if (result == 3 && up)
				{
					CPU_iter->SetDirection(NORTH);
					return OK;
				}
				else if (result == 4 && down)
				{
					CPU_iter->SetDirection(SOUTH);
					return OK;
				}
			}
		}
	}

	return OK;
}

int CModel::Theft(vector<CHuman>::iterator human)
{
	int result;
	while(true)
	{
		result = Random(1, m_iNumberPlayers);
		if (result != human->GetOrder())
			break;
	}

	vector<CCPU>::iterator CPU_iter;
	vector<CHuman>::iterator Human_iter;

	for (Human_iter = m_Human.begin(); Human_iter != m_Human.end(); Human_iter++)
	{
		if  (Human_iter->GetOrder() == result)
		{
			if (Human_iter->GetBalance() <= 0 || human->GetBalance() <= 0)
			{
				human->SetState(END);
				return OK;
			}
			else
			{
				if (human->GetBalance() > Human_iter->GetBalance())
				{
					int money = Random(1, human->GetBalance());

					if (Random(1, 2) == 1)
					{
						human->AddBalance(money);
						Human_iter->SubBalance(money);
						human->AddRob(money);
						human->SetState(END);
						return OK;
					}
					else
					{
						human->SubBalance(money);
						Human_iter->AddBalance(money);
						human->SubRob(money);
						human->SetState(END);
						return OK;
					}
				}
				else
				{
					int money = Random(1, Human_iter->GetBalance());

					if (Random(1, 2) == 1)
					{
						human->AddBalance(money);
						Human_iter->SubBalance(money);
						human->AddRob(money);
						human->SetState(END);
						return OK;
					}
					else
					{
						human->SubBalance(money);
						Human_iter->AddBalance(money);
						human->SubRob(money);
						human->SetState(END);
						return OK;
					}
				}
			}
		}
	}

	for (CPU_iter = m_CPU.begin(); CPU_iter != m_CPU.end(); CPU_iter++)
	{
		if (CPU_iter->GetOrder() == result)
		{
			if (CPU_iter->GetBalance() <= 0 || human->GetBalance() <= 0)
			{
				human->SetState(END);
				return OK;
			}
			else
			{
				if (human->GetBalance() > CPU_iter->GetBalance())
				{
					int money = Random(1, human->GetBalance());

					if (Random(1, 2) == 1)
					{
						human->AddBalance(money);
						CPU_iter->SubBalance(money);
						human->AddRob(money);
						human->SetState(END);
						return OK;
					}
					else
					{
						human->SubBalance(money);
						CPU_iter->AddBalance(money);
						human->SubRob(money);
						human->SetState(END);
						return OK;
					}
				}
				else
				{
					int money = Random(1, CPU_iter->GetBalance());

					if (Random(1, 2) == 1)
					{
						human->AddBalance(money);
						CPU_iter->SubBalance(money);
						human->AddRob(money);
						human->SetState(END);
						return OK;
					}
					else
					{
						human->SubBalance(money);
						CPU_iter->AddBalance(money);
						human->SubRob(money);
						human->SetState(END);
						return OK;
					}
				}
			}
		}
	}

	return OK;
}

int CModel::Theft(vector<CCPU>::iterator CPU)
{
	int result;
	while(true)
	{
		result = Random(1, m_iNumberPlayers);
		if (result != CPU->GetOrder())
			break;
	}

	vector<CCPU>::iterator CPU_iter;
	vector<CHuman>::iterator Human_iter;

	for (Human_iter = m_Human.begin(); Human_iter != m_Human.end(); Human_iter++)
	{
		if  (Human_iter->GetOrder() == result)
		{
			if (Human_iter->GetBalance() <= 0 || CPU->GetBalance() <= 0)
			{
				CPU->SetState(_END);
				return OK;
			}
			else
			{
				if (CPU->GetBalance() > Human_iter->GetBalance())
				{
					int money = Random(1, CPU->GetBalance());

					if (Random(1, 2) == 1)
					{
						CPU->AddBalance(money);
						Human_iter->SubBalance(money);
						CPU->AddRob(money);
						CPU->SetState(_END);
						return OK;
					}
					else
					{
						CPU->SubBalance(money);
						Human_iter->AddBalance(money);
						CPU->SubRob(money);
						CPU->SetState(_END);
						return OK;
					}
				}
				else
				{
					int money = Random(1, Human_iter->GetBalance());

					if (Random(1, 2) == 1)
					{
						CPU->AddBalance(money);
						Human_iter->SubBalance(money);
						CPU->AddRob(money);
						CPU->SetState(_END);
						return OK;
					}
					else
					{
						CPU->SubBalance(money);
						Human_iter->AddBalance(money);
						CPU->SubRob(money);
						CPU->SetState(_END);
						return OK;
					}
				}
			}
		}
	}

	for (CPU_iter = m_CPU.begin(); CPU_iter != m_CPU.end(); CPU_iter++)
	{
		if (CPU_iter->GetOrder() == result)
		{
			if (CPU_iter->GetBalance() <= 0 || CPU->GetBalance() <= 0)
			{
				CPU->SetState(_END);
				return OK;
			}
			else
			{
				if (CPU->GetBalance() > CPU_iter->GetBalance())
				{
					int money = Random(1, CPU->GetBalance());

					if (Random(1, 2) == 1)
					{
						CPU->AddBalance(money);
						CPU_iter->SubBalance(money);
						CPU->AddRob(money);
						CPU->SetState(_END);
						return OK;
					}
					else
					{
						CPU->SubBalance(money);
						CPU_iter->AddBalance(money);
						CPU->SubRob(money);
						CPU->SetState(_END);
						return OK;
					}
				}
				else
				{
					int money = Random(1, CPU_iter->GetBalance());

					if (Random(1, 2) == 1)
					{
						CPU->AddBalance(money);
						CPU_iter->SubBalance(money);
						CPU->AddRob(money);
						CPU->SetState(_END);
						return OK;
					}
					else
					{
						CPU->SubBalance(money);
						CPU_iter->AddBalance(money);
						CPU->SubRob(money);
						CPU->SetState(_END);
						return OK;
					}
				}
			}
		}
	}
	
	return OK;
}

int CModel::DirectionCPU(vector<CCPU>::iterator CPU)
{
	bool right = false;
	bool left = false;
	bool up = false;
	bool down = false;
	int result = 0;

	//CheckDirection(CPU);
	CPU->GetPossibleDirection(right, left, up, down);

	while(true)
	{
		result = Random(0, 3);
		if (result == NORTH && up)
		{
			CPU->SetDirection(NORTH);
			return OK;
		}
		else if (result == SOUTH && down)
		{
			CPU->SetDirection(SOUTH);
			return OK;
		}
		else if (result == EAST && right)
		{
			CPU->SetDirection(EAST);
			return OK;
		}
		else if (result == WEST && left)
		{
			CPU->SetDirection(WEST);
			return OK;
		}
	}

	return OK;
}

int CModel::MoveIcon(vector<CHuman>::iterator human)
{
	POINT a;
	bool right = false;
	bool left = false;
	bool up = false;
	bool down = false;

	if (human->GetDirection() == NOWHERE)
	{
		human->SetState(DIRECTION);
		return OK;
	}

	while (true)
	{
		CheckDirection(human);

		right = false;
		left = false;
		up = false;
		down = false;

		human->GetPossibleDirection(right, left, up, down);

		// check safe house
		a = human->GetPosition();
		if (m_vMap[m_iMapWidth * a.y + a.x] == '7' && human->GetBalance() >= MAX)
		{
			m_PlayState = WINNING;
			return OK;
		}

		//-------------------------------------------

		if (human->GetDirection() == NORTH && human->JustAsk())
		{
			a = human->GetPosition();
			a.y--;
			human->SetPosition(a);
			human->Count();
			human->ClearAsk();
		}
		else if (human->GetDirection() == NORTH && up && !right && !left)
		{
			a = human->GetPosition();
			a.y--;
			human->SetPosition(a);
			human->Count();
		}
		else if (human->GetDirection() == NORTH && !up)
		{
			if (!right && left)
			{
				human->SetDirection(WEST);
				a = human->GetPosition();
				a.x--;
				human->SetPosition(a);
				human->Count();
			}
			else if (right && !left)
			{
				human->SetDirection(EAST);
				a = human->GetPosition();
				a.x++;
				human->SetPosition(a);
				human->Count();
			}
			else if (right && left)
			{
				human->SetState(DIRECTION);
				human->SetPossibleDirection(right, left, false, false);
				return OK;
			}
		}
		else if (human->GetDirection() == NORTH && up && !right && left)
		{
			human->SetState(DIRECTION);
			human->SetPossibleDirection(false, left, up, false);
			return OK;
		}
		else if (human->GetDirection() == NORTH && up && right && !left)
		{
			human->SetState(DIRECTION);
			human->SetPossibleDirection(right, false, up, false);
			return OK;
		}
		else if (human->GetDirection() == NORTH && up && right && left)
		{
			human->SetState(DIRECTION);
			human->SetPossibleDirection(right, left, up, false);
			return OK;
		}

		//--------------------------------------

		else if (human->GetDirection() == EAST && human->JustAsk())
		{
			a = human->GetPosition();
			a.x++;
			human->SetPosition(a);
			human->Count();
			human->ClearAsk();
		}
		else if (human->GetDirection() == EAST && right && !up && !down)
		{
			a = human->GetPosition();
			a.x++;
			human->SetPosition(a);
			human->Count();
		}
		else if (human->GetDirection() == EAST && !right)
		{
			if (!up && down)
			{
				human->SetDirection(SOUTH);
				a = human->GetPosition();
				a.y++;
				human->SetPosition(a);
				human->Count();
			}
			else if (up && !down)
			{
				human->SetDirection(NORTH);
				a = human->GetPosition();
				a.y--;
				human->SetPosition(a);
				human->Count();
			}
			else if (up && down)
			{
				human->SetState(DIRECTION);
				human->SetPossibleDirection(false, false, up, down);
				return OK;
			}
		}
		else if (human->GetDirection() == EAST && up && !down && right)
		{
			human->SetState(DIRECTION);
			human->SetPossibleDirection(right, false, up, false);
			return OK;
		}
		else if (human->GetDirection() == EAST && !up && down && right)
		{
			human->SetState(DIRECTION);
			human->SetPossibleDirection(right, false, false, down);
			return OK;
		}
		else if (human->GetDirection() == EAST && up && down && right)
		{
			human->SetState(DIRECTION);
			human->SetPossibleDirection(right, false, up, down);
			return OK;
		}

		//-----------------------------------------------

		else if (human->GetDirection() == SOUTH && human->JustAsk())
		{
			a = human->GetPosition();
			a.y++;
			human->SetPosition(a);
			human->Count();
			human->ClearAsk();
		}
		else if (human->GetDirection() == SOUTH && down && !right && !left)
		{
			a = human->GetPosition();
			a.y++;
			human->SetPosition(a);
			human->Count();
		}
		else if (human->GetDirection() == SOUTH && !down)
		{
			if (!right && left)
			{
				human->SetDirection(WEST);
				a = human->GetPosition();
				a.x--;
				human->SetPosition(a);
				human->Count();
			}
			else if (right && !left)
			{
				human->SetDirection(EAST);
				a = human->GetPosition();
				a.x++;
				human->SetPosition(a);
				human->Count();
			}
			else if (right && left)
			{
				human->SetState(DIRECTION);
				human->SetPossibleDirection(right, left, false, false);
				return OK;
			}
		}
		else if (human->GetDirection() == SOUTH && down && !right && left)
		{
			human->SetState(DIRECTION);
			human->SetPossibleDirection(false, left, false, down);
			return OK;
		}
		else if (human->GetDirection() == SOUTH && down && right && !left)
		{
			human->SetState(DIRECTION);
			human->SetPossibleDirection(right, false, false, down);
			return OK;
		}
		else if (human->GetDirection() == SOUTH && down && right && left)
		{
			human->SetState(DIRECTION);
			human->SetPossibleDirection(right, left, false, down);
			return OK;
		}

		//--------------------------------------

		else if (human->GetDirection() == WEST && human->JustAsk())
		{
			a = human->GetPosition();
			a.x--;
			human->SetPosition(a);
			human->Count();
			human->ClearAsk();
		}
		else if (human->GetDirection() == WEST && left && !up && !down)
		{
			a = human->GetPosition();
			a.x--;
			human->SetPosition(a);
			human->Count();
		}
		else if (human->GetDirection() == WEST && !left)
		{
			if (!up && down)
			{
				human->SetDirection(SOUTH);
				a = human->GetPosition();
				a.y++;
				human->SetPosition(a);
				human->Count();
			}
			else if (up && !down)
			{
				human->SetDirection(NORTH);
				a = human->GetPosition();
				a.y--;
				human->SetPosition(a);
				human->Count();
			}
			else if (up && down)
			{
				human->SetState(DIRECTION);
				human->SetPossibleDirection(false, false, up, down);
				return OK;
			}
		}
		else if (human->GetDirection() == WEST && !up && down && left)
		{
			human->SetState(DIRECTION);
			human->SetPossibleDirection(false, left, false, down);
			return OK;
		}
		else if (human->GetDirection() == WEST && up && !down && left)
		{
			human->SetState(DIRECTION);
			human->SetPossibleDirection(false, left, up, false);
			return OK;
		}
		else if (human->GetDirection() == WEST && up && down && left)
		{
			human->SetState(DIRECTION);
			human->SetPossibleDirection(false, left, up, down);
			return OK;
		}
	
		//----------------------------------------------------------

		if (human->DoneMove())
		{
			a = human->GetPosition();
			if (m_vMap[m_iMapWidth * a.y + a.x] == '3')
			{
				human->AddRob(5);
				human->AddBalance(5);
				human->SetState(END);
			}
			else if (m_vMap[m_iMapWidth * a.y + a.x] == '2')
			{
				human->AddRob(10);
				human->AddBalance(10);
				human->SetState(END);
			}
			else if (m_vMap[m_iMapWidth * a.y + a.x] == '4')
			{
				human->AddRob(20);
				human->AddBalance(20);
				human->SetState(END);
			}
			else if (m_vMap[m_iMapWidth * a.y + a.x] == '6')
			{
				Theft(human);
				//human->SetState(END);
			}
			else if (m_vMap[m_iMapWidth * a.y + a.x] == '5')
			{
				Tip();
				human->SetState(END);
			}
			else
				human->SetState(END);	
			return OK;
		}
	}
	return OK;
}

void CModel::CheckDirection(vector<CHuman>::iterator human)
{
	POINT position;
	position = human->GetPosition();

	int x = position.x;
	int y = position.y;

	bool right = false;
	bool left = false;
	bool up = false;
	bool down = false;

	// check up
	if (y > 0 && m_vMap[(y - 1) * m_iMapWidth + x] != '0')
		up = true;
	// right
	if (x < m_iMapWidth - 1 && m_vMap[y * m_iMapWidth + 1 + x] != '0')
		right = true;
	// down
	if (y < m_iMapHeight - 1 && m_vMap[(y + 1) * m_iMapWidth + x] != '0')
		down = true;
	// left
	if (x > 0 && m_vMap[y * m_iMapWidth - 1 + x] != '0')
		left = true;	
		
	human->SetPossibleDirection(right, left, up, down);
}

void CModel::CheckDirection(vector<CCPU>::iterator CPU)
{
	POINT position;
	position = CPU->GetPosition();

	int x = position.x;
	int y = position.y;

	bool right = false;
	bool left = false;
	bool up = false;
	bool down = false;

	// check up
	if (y > 0 && m_vMap[(y - 1) * m_iMapWidth + x] != '0')
		up = true;
	// right
	if (x < m_iMapWidth - 1 && m_vMap[y * m_iMapWidth + 1 + x] != '0')
		right = true;
	// down
	if (y < m_iMapHeight - 1 && m_vMap[(y + 1) * m_iMapWidth + x] != '0')
		down = true;
	// left
	if (x > 0 && m_vMap[y * m_iMapWidth - 1 + x] != '0')
		left = true;	
		
	CPU->SetPossibleDirection(right, left, up, down);
}

int CModel::GetPlayState()
{
	return m_PlayState;
}

int CModel::GetNext()
{
	return m_iNext;
}

int CModel::Random(int min, int max)
{
	max++;
	return (int)((max - min) * rand()/(float)RAND_MAX + min);
}

int CModel::SetPositionCPU()
{
	vector<CCPU>::iterator CPU_iter;

	for (CPU_iter = m_CPU.begin(); CPU_iter != m_CPU.end(); CPU_iter++)
	{
		if (CPU_iter->GetOrder() == m_iNext)
		{
			int num = Random(1, 4);
			int count = 0;
			POINT a;
			if (num == 1)
			{
				for (int i = 0; i < m_iMapHeight; i++)
				{
					for (int h = 0; h < m_iMapWidth; h++)
						if (m_vMap[count++] == '1')
						{
							a.x = h;
							a.y = i;
						}
				}
				CPU_iter->SetPosition(a);
			}
			if (num == 2)
			{
				for (int i = 0; i < m_iMapHeight; i++)
				{
					for (int h = 0; h < m_iMapWidth; h++)
						if (m_vMap[count++] == '9')
						{
							a.x = h;
							a.y = i;
						}
				}
				CPU_iter->SetPosition(a);
			}
			if (num == 3)
			{
				for (int i = 0; i < m_iMapHeight; i++)
				{
					for (int h = 0; h < m_iMapWidth; h++)
						if (m_vMap[count++] == 'a')
						{
							a.x = h;
							a.y = i;
						}
				}
				CPU_iter->SetPosition(a);
			}
			if (num == 4)
			{
				for (int i = 0; i < m_iMapHeight; i++)
				{
					for (int h = 0; h < m_iMapWidth; h++)
						if (m_vMap[count++] == 'b')
						{
							a.x = h;
							a.y = i;
						}
				}
				CPU_iter->SetPosition(a);
			}
			m_iNext++;
			if (m_iNext > m_iNumberPlayers)
			{
				// change state
				m_PlayState = GAME;
				m_iNext = 1;
			}
			return OK;
		}
	}

	return OK;
}

int CModel::SetPosition(int num)
{
	// use iterator instead count human and cpu because save and load factors
	vector<CHuman>::iterator Human_iter;

	for (Human_iter = m_Human.begin(); Human_iter != m_Human.end(); Human_iter++)
	{
		if (Human_iter->GetOrder() == m_iNext)
		{
			int count = 0;
			POINT a;
			if (num == 1)
			{
				for (int i = 0; i < m_iMapHeight; i++)
				{
					for (int h = 0; h < m_iMapWidth; h++)
						if (m_vMap[count++] == '1')
						{
							a.x = h;
							a.y = i;
						}
				}
				Human_iter->SetPosition(a);
			}
			if (num == 2)
			{
				for (int i = 0; i < m_iMapHeight; i++)
				{
					for (int h = 0; h < m_iMapWidth; h++)
						if (m_vMap[count++] == '9')
						{
							a.x = h;
							a.y = i;
						}
				}
				Human_iter->SetPosition(a);
			}
			if (num == 3)
			{
				for (int i = 0; i < m_iMapHeight; i++)
				{
					for (int h = 0; h < m_iMapWidth; h++)
						if (m_vMap[count++] == 'a')
						{
							a.x = h;
							a.y = i;
						}
				}
				Human_iter->SetPosition(a);
			}
			if (num == 4)
			{
				for (int i = 0; i < m_iMapHeight; i++)
				{
					for (int h = 0; h < m_iMapWidth; h++)
						if (m_vMap[count++] == 'b')
						{
							a.x = h;
							a.y = i;
						}
				}
				Human_iter->SetPosition(a);
			}
			m_iNext++;
			if (m_iNext > m_iNumberPlayers)
			{
				// change state
				m_PlayState = GAME;
				m_iNext = 1;
			}
			return OK;
		}
	}	
	return OK;
}

void CModel::SetNumberPlayer(int num)
{
	m_iNumberPlayers = num;
	m_PlayState = SETPLAYER;
}

void CModel::SetHuman()
{
	CHuman a;
	cout << "Please type your name for Player " << m_iNext << ": ";
	char name[20];
	cin >> name;
	a.SetName(name);

	
	if (m_iNext == 1)
	{
		a.SetIcon(1); // symbol white smile face
		a.SetOrder(1);
		m_Human.push_back(a);
	}
	else if (m_iNext == 2)
	{
		a.SetIcon(2); // symbol black smile face	
		a.SetOrder(2);
		m_Human.push_back(a);
	}
	else if (m_iNext == 3)
	{
		a.SetIcon(4); // symbol diamond
		a.SetOrder(3);
		m_Human.push_back(a);
	}
	else if (m_iNext == 4)
	{
		a.SetIcon(11); // symbol male	
		a.SetOrder(4);
		m_Human.push_back(a);
	}
	else if (m_iNext == 5)
	{
		a.SetIcon(12); // symbol female	
		a.SetOrder(5);
		m_Human.push_back(a);
	}
	else if (m_iNext == 6)
	{
		a.SetIcon(15); // symbol star
		a.SetOrder(6);
		m_Human.push_back(a);
	}

	if (++m_iNext > m_iNumberPlayers)
		m_PlayState = STATUS;
}

void CModel::SetCPU()
{
	CCPU a;
	if (m_iNext == 1)
	{
		a.SetIcon(1); // symbol white smile face
		a.SetName("CPU 1");		
		a.SetOrder(1);
		m_CPU.push_back(a);
	}
	else if (m_iNext == 2)
	{
		a.SetIcon(2); // symbol black smile face
		a.SetName("CPU 2");	
		a.SetOrder(2);
		m_CPU.push_back(a);
	}
	else if (m_iNext == 3)
	{
		a.SetIcon(4); // symbol diamond
		a.SetName("CPU 3");	
		a.SetOrder(1);
		a.SetOrder(3);
		m_CPU.push_back(a);
	}
	else if (m_iNext == 4)
	{
		a.SetIcon(11); // symbol male
		a.SetName("CPU 4");		
		a.SetOrder(4);
		m_CPU.push_back(a);
	}
	else if (m_iNext == 5)
	{
		a.SetIcon(12); // symbol female
		a.SetName("CPU 5");	
		a.SetOrder(5);
		m_CPU.push_back(a);
	}
	else if (m_iNext == 6)
	{
		a.SetIcon(15); // symbol star
		a.SetName("CPU 6");		
		a.SetOrder(6);
		m_CPU.push_back(a);
	}

	if (++m_iNext > m_iNumberPlayers)
		m_PlayState = STATUS;
}

void CModel::GetPlayers(vector<CCPU> &CPU, vector<CHuman> &Human)
{
	CPU.clear();
	Human.clear();

	CPU = m_CPU;
	Human = m_Human;
}
